class ProfitStatement < ActiveRecord::Base
  @@cn2en = {
    '报告期'=>'report_date',
    
    # 一、主营业务收入
    '主营业务收入' => 'main_operating_revenue',
    '折扣与折让' => 'discount_and_allowance',
    '主营业务收入净额'  => 'net_income_from_main_operation',
    '主营业务成本'  => 'operating_costs',
    '主营业务税金及附加'  => 'sales_tax',
    
    # 二、主营业务利润
    '主营业务利润'  => 'main_operating_profit',
    '其他业务利润'  => 'other_operation_profit',
    '存货跌价损失'  => 'loss_on_inventory',
    '营业费用'  => 'operating_expenses',
    '管理费用' => 'management_fee',
    '财务费用'  => 'financial_expenses',
    
    # 三、营业利润
    '营业利润'  => 'operating_profit',
    '投资收益'  => 'return_on_investment',
    '补贴收入' => 'subsidy_income',
    '营业外收入'  => 'non_operating_income',
    '营业外支出'  => 'non_operation_expenses',
    '以前年度损益调整'  => 'prior_year_income_adjustment',
    '营业外收支净额'  => 'net_non_operating_income',
    
  	# 四、利润总额
    '利润总额'  => 'total_profit',
    '所得税'  => 'incoming_tax',
    '财政返还'  => 'financial_return',
    '少数股东权益'  => 'minority_equity',
    '购买日前净利润'  => 'net_profit_before_buy',
    '未确认的投资损失'  => 'unrecognized_investment_losses',
    
    # 五、净利润
    '净利润'  => 'net_profit',
    '年初未分配利润'  => 'profit_available_for_distribution',
    '调整以前年度损益'  => 'prior_year_income_adjustment',
    '盈余公积转入'  => 'reserve_into',
    '住房周转金转入'  => 'revolving_fund_of_house_into',
    '其他转入'  => 'other_into',
    
    # 六、可分配利润
    '可分配利润'  => 'attributable_profit',
    '提取法定盈余公积'  => 'withdrawal_legal_surplus',
    '提取法定公益金'  => 'withdrawal_legal_public_welfare_funds',
    '职工奖金福利'  => 'welfare_workers_bonuses',
    '提取储备基金'  => 'withdrawal_reserve_fund',
    '提取企业发展基金'  => 'withdrawal_reserve_for_business_expansion',
    
    # 七、可供股东分配的利润
    '可供股东分配的利润'  => 'distribution_of_profits_for_shareholders',
    '应付优先股股利'  => 'preferred_stock_dividends_payable',
    '提取任意盈余公积金'  => 'extraction_of_any_surplus_fund',
    '应付普通股股利'  => 'common_stock_dividends_payable',
    '转作股本的普通股股利'  => 'dividends_transferred_to_capital',
    
    # 八、未分配利润
    '未分配利润'  => 'undivided_profit'
  }
  
  @@en2cn = {}

	def self.cn2en
		@@cn2en
	end

  def self.en2cn
    if @@en2cn.empty?
      @@cn2en.each { |key, value| @@en2cn[value] = key }
    end
    @@en2cn
  end
	
	def self.find_by_stock_no_from_sina(stock_no)
		return Util.find_data_by_from_sina(
			"http://money.finance.sina.com.cn/corp/go.php/vFD_ProfitStatement/stockid/#{stock_no}/ctrl/part.phtml",
			'ProfitStatementNewTable',
			@@cn2en
		)
  end
  
  def net_profit_margin
    net_profit.to_f / main_operating_revenue.to_f 
  end
  
  def gross_profit_margin
    main_operating_profit.to_f / main_operating_revenue.to_f
  end
  
  def operating_profit_margin
    operating_profit.to_f / main_operating_revenue.to_f
  end
end
